package org.dstart.air.window.sub
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import org.dstart.air.window.sub.draw.*;

	public class WindowBackground extends Sprite
	{
		public var sprTitle:Sprite;			//	标题
		public var sprBg:Sprite;			//	背景对象
		public var btnClose:SimpleButton;	//	关闭按钮
		public var btnMax:SimpleButton;		//	最大化按钮
		public var btnMin:SimpleButton;		//	最小化按钮
		public var btnReturn:SimpleButton;	//	还原按钮
		public var btnResize:SimpleButton;	//	调节大小
		public var sprIcon:Sprite;			//	标题图标
		
		private var txtTitle:TextField;		//	标题文本
		private var bmpIcon:Bitmap;			//	最小化后显示的位图
		
		public function WindowBackground()
		{
			InitObject();
			InitListener();
		}
		
		public function ChangeSize(ww:Number, hh:Number):void
		{
			sprBg.width = ww;
			sprBg.height = hh;
			
			sprTitle.width = ww;
			
			btnClose.y = btnMax.y = btnMin.y = 6;
			
			sprIcon.x = 5;
			sprIcon.y = 5;
			
			if(sprIcon.visible)
			{
				txtTitle.x = 25;
				txtTitle.y = 4;
			}
			else
			{
				txtTitle.x = 6;
				txtTitle.y = 5;
			}			
			
			btnClose.x = sprBg.width - btnClose.width - 6;
			btnMax.x = btnClose.x - btnMax.width - 2;
			btnReturn.x = btnMax.x;
			btnReturn.y = btnMax.y;
			
			if(btnMax.visible || btnReturn.visible)
			{
				btnMin.x = btnMax.x - btnMin.width - 2;
			}
			else
			{
				this.btnMin.x - this.btnClose.x - this.btnMin.width - 2;//btnMin.x  btnClose.x - btnMin.width - 2;
			}
			
			btnResize.x = sprBg.width - btnResize.width - 2;
			btnResize.y = sprBg.height - btnResize.height - 2;
		}
		
		/** 是否显示左侧的图标  **/
		public function set showIcon(flag:Boolean):void
		{
			this.sprIcon.visible = flag;
			
			txtTitle.x = flag ? 30 : 6;
		}
		
		/** 是否允许修改大小 **/
		public function set canResize(flag:Boolean):void
		{
			this.btnMax.visible = flag;			
			this.btnResize.visible = flag;
			
			if(this.btnMin.visible)
			{
				this.btnMin.x = this.btnResize.x - this.btnMin.width - 2;
			}			
		}
		
		/** 是否允许最小化 **/
		public function set canMin(flag:Boolean):void
		{
			this.btnMin.visible = flag;
			
			if(this.btnResize.visible)
			{
				this.btnMin.x =  this.btnResize.x - this.btnMin.width - 2;
			}
			else
			{
				this.btnMin.x = this.btnClose.x - this.btnMin.width - 2;
			}
		}
		
		/** 是否允许拖动  **/
		public function set canMove(flag:Boolean):void
		{
			this.sprTitle.visible = flag;
		}
		
		/** 是否可以关闭窗口 **/
		public function set canClose(flag:Boolean):void
		{
			this.btnClose.visible = flag;
		}		
		
		/** 取得位图对象 **/
		public function get bitmap():BitmapData
		{
			var bmp:BitmapData = new BitmapData(sprIcon.width, sprIcon.height, true, 0xFFFFFF);
			bmp.draw(sprIcon);
			
			return bmp;
		}
		
		/** 设置标题文本 **/
		public function set title(str:String):void
		{
			txtTitle.width = 1000;
			txtTitle.height = 18;
			txtTitle.text = str;
			txtTitle.selectable = false;
			txtTitle.width = txtTitle.textWidth + 6;
		}
		
		/***************************************************************/
		
		private function InitObject():void
		{
			if(!sprTitle || !sprBg || !btnClose || !btnMax || !btnMin || !btnReturn || !btnResize || !sprIcon)
			{
				sprBg = new DrawBackground();
				sprTitle = new DrawTitle();
				btnClose = new DrawCloseButton();
				btnMax = new DrawMaxButton();
				btnMin = new DrawMinButton();
				btnReturn = new DrawReturnButton();		
				btnResize = new DrawResizeButton();
				sprIcon = new DrawBaseIcon();
				
				addChild(sprBg);
				addChild(sprTitle);
				addChild(btnMin);
				addChild(btnMax);
				addChild(btnReturn);
				addChild(btnClose);
				addChild(btnResize);
				addChild(sprIcon);				
			}
			
			txtTitle = new TextField();
			txtTitle.height = 18;
			txtTitle.selectable = false;
			txtTitle.mouseEnabled = false;
			
			this.addChild(txtTitle);
			
			this.btnReturn.visible = false;
			this.title = "AIR Window";
		}
		
		
		private function InitListener():void
		{
			this.sprTitle.doubleClickEnabled = true;
			this.sprTitle.addEventListener(MouseEvent.DOUBLE_CLICK, onCSizeWindow);
			this.sprTitle.addEventListener(MouseEvent.MOUSE_DOWN, onDragWindow);
			this.btnClose.addEventListener(MouseEvent.CLICK, onCloseWindow);
			this.btnMax.addEventListener(MouseEvent.CLICK, onMaxWindow);
			this.btnMin.addEventListener(MouseEvent.CLICK, onMinWindow);
			this.btnReturn.addEventListener(MouseEvent.CLICK, onReturnWindow);
			this.btnResize.addEventListener(MouseEvent.MOUSE_DOWN, onResizeWindow);
		}
		
		public static const DRAG_WINDOWS:String		= "dragWindows";
		public static const CLOSE_WINDOWS:String	= "closeWindows";
		public static const MAX_WINDOWS:String		= "maxWindows";
		public static const MIN_WINDOWS:String		= "minWindows";
		public static const RETURN_WINDOWS:String	= "returnWindows";
		public static const RESIZE_WINDOWS:String	= "resizeWindows";
		
		//	双击标题
		private function onCSizeWindow(e:MouseEvent):void
		{
			if(btnMax.visible)
			{
				onMaxWindow(null);
			}
			else if(btnReturn.visible)
			{
				onReturnWindow(null);
			}
		}
		
		//	拖动标题
		private function onDragWindow(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(DRAG_WINDOWS));
		}

		//	关闭窗口
		private function onCloseWindow(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(CLOSE_WINDOWS));
		}

		//	最大化
		private function onMaxWindow(e:MouseEvent):void
		{
			this.btnMax.visible = false;
			this.btnReturn.visible = true;
			
			this.dispatchEvent(new Event(MAX_WINDOWS));
		}
		
		//	最小化
		private function onMinWindow(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(MIN_WINDOWS));
		}

		//	还原窗口
		private function onReturnWindow(e:MouseEvent):void
		{
			this.btnMax.visible = true;
			this.btnReturn.visible = false;
			
			this.dispatchEvent(new Event(RETURN_WINDOWS));
		}

		//	调节窗口大小
		private function onResizeWindow(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(RESIZE_WINDOWS));
		}
	}
}